<template>
  <div class="graphTrend-pack">
    <el-row>
      <el-col :span="24">
        <ele-form
          v-model="searchData"
          :formDesc="reportLineSearchConfig"
          :inline="true"
          :isShowSubmitBtn="false"
          ref="eleTable"
        >
          <template v-slot:searchBtn>
            <el-button
              type="primary"
              @click="searchRecord"
              :loading="btnLoading"
            >
              {{ $i18n.t("common.search") }}
            </el-button>
          </template>
          <template v-slot:resetBtn>
            <el-button
              @click="handleReset"
              :loading="btnLoading"
              icon="el-icon-refresh"
            >
              {{ $i18n.t("common.reset") }}
            </el-button>
          </template>
          <!-- <el-button @click="handleReset" icon="el-icon-refresh">{{ $t('common.reset') }}</el-button> -->
        </ele-form>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="footer">
          <p>
            {{ $i18n.t("abnormalProcessAnalysis.processAnalysisRejectRate") }}
          </p>
        </div>
        <div
          id="liquid-chart"
          class="liquid-chart"
          :v-bind="$attrs"
          v-echartResize="resize"
          :style="{ height: '35vh' }"
        ></div>
      </el-col>
      <el-col :span="24">
        <div class="footer">
          <p>
            {{
              $i18n.t("abnormalProcessAnalysis.processAnalysisDefectiveRate")
            }}
          </p>
        </div>
        <div
          id="liquid-chart2"
          class="liquid-chart"
          :v-bind="$attrs"
          v-echartResize="resize2"
          :style="{ height: '35vh' }"
        ></div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from "echarts";
import { reportLineSearchConfig } from "./config";
import { getProcessExceptionAnalysisLine } from "@/api/report/mesProdOrderPlanPassRatio";
import moment from "moment";
export default {
  name: "graphTrend",
  mixins: [reportLineSearchConfig],
  data() {
    return {
      searchData: {},
      btnLoading: false,
      option: null,
      chart: null,
      chart2: null,
      workdayList: [],
      openRatioList: [],
      closeRatioList: [],
    };
  },
  // mounted() {
  //   this.searchRecord(1)
  // },
  methods: {
    searchRecord(flag) {
      if (flag == 1) {
        this.timeChange("4");
      }
      const actualEndDate = this.searchData.actualEndDate;
      let params =
        actualEndDate && actualEndDate.length > 1
          ? {
              workshopCode: this.searchData.workshopCode,
              lineCode: this.searchData.lineCode,
              actualEndDateMin: actualEndDate[0],
              actualEndDateMax: actualEndDate[1],
            }
          : {
              searchTimeType: this.searchData.searchTimeType,
              workshopCode: this.searchData.workshopCode,
              lineCode: this.searchData.lineCode,
            };
      getProcessExceptionAnalysisLine(params).then((res) => {
        if (res.data.success) {
          this.workdayList = res.data.result.workdayList;
          this.openRatioList = res.data.result.openRatioList;
          this.closeRatioList = res.data.result.closeRatioList;
          this.init();
          this.init2();
          // this.setChartBox(res.data.result)
        }
      });
    },
    resize() {
      if (this.chart) {
        this.chart.resize();
      }
    },
    resize2() {
      if (this.chart) {
        this.chart2.resize();
      }
    },
    handleReset() {
      this.searchData = {
        actualEndDate: [
          moment().subtract(8, "days").format("YYYY-MM-DD"),
          moment().subtract(1, "days").format("YYYY-MM-DD"),
        ],
        searchTimeType: "4",
      };
    },
    timeChange(val) {
      if (val == "4") {
          this.$set(this.searchData,'searchTimeType',val)
          this.searchData.actualEndDate = [
            moment().subtract(8, "days").format("YYYY-MM-DD"),
            moment().subtract(1, "days").format("YYYY-MM-DD"),
          ];
        } else {
          this.$set(this.searchData,'searchTimeType',val)
          this.searchData.actualEndDate = [
            moment().subtract(31, "days").format("YYYY-MM-DD"),
            moment().subtract(1, "days").format("YYYY-MM-DD"),
          ];
      }
    },
    init() {
      this.option = {
        title: {
          text: "",
        },
        tooltip: {
          trigger: "axis",
          formatter: function (params) {
            var str ='';
            for (let item of params) {
              str += item.name + " : " + item.value + "%<br>";
            }
            return str;
          },
        },
        // legend: {
        //   data: [$i18n.t("abnormalProcessAnalysis.scrapRatio")],
        // },
        grid: {
          left: "3%",
          right: "4%",
          top: "4%",
          bottom: "7%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.workdayList,
          // data: ['2024-01-16', '2024-01-17', '2024-01-18', '2024-01-19', '2024-01-20', '2024-01-21', '2024-01-22']
        },
        yAxis: [
          {
            name: "",
            type: "value",
            axisLabel: {
              show: true,
              formatter: function (value) {
                // 在标签后面添加百分号
                return value + "%";
              },
            },
          },
          // { name: '', type: 'value' }
        ],
        series: [
          {
            name: $i18n.t("abnormalProcessAnalysis.scrapRatio"),
            type: "line",
            stack: "Total",
            data: this.openRatioList,
            lineStyle: {
              color: "#F26A60",
            },
            itemStyle: {
              normal: {
                color: "#F26A60", //拐点颜色
              },
            },
          },
        ],
      };
      this.chart = echarts.init(document.getElementById("liquid-chart"));
      this.chart.setOption(this.option);
    },
    init2() {
      let option = {
        title: {
          text: "",
        },
        tooltip: {
          trigger: "axis",
          formatter: function (params) {
            var str ='';
            for (let item of params) {
              str += item.name + " : " + item.value + "%<br>";
            }
            return str;
          },
        },
        // legend: {
        //   data: [$i18n.t("abnormalProcessAnalysis.timeToCompleteTitle")],
        // },
        grid: {
          left: "3%",
          right: "4%",
          top: "4%",
          bottom: "7%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.workdayList,
     
          // data: ['2024-01-16', '2024-01-17', '2024-01-18', '2024-01-19', '2024-01-20', '2024-01-21', '2024-01-22']
        },
        yAxis: [
          {
            name: "",
            type: "value",
            axisLabel: {
              show: true,
              formatter: function (value) {
                // 在标签后面添加百分号
                return value + "%";
              },
            },
          },
        ],
        series: [
          {
            name: $i18n.t("abnormalProcessAnalysis.timeToCompleteTitle"),
            type: "line",
            stack: "Total",
            data: this.closeRatioList,
            lineStyle: {
              color: "#00BFA5",
            },
            itemStyle: {
              normal: {
                color: "#00BFA5", //拐点颜色
              },
            },
          },
        ],
      };
      this.chart2 = echarts.init(document.getElementById("liquid-chart2"));
      this.chart2.setOption(option);
    },
  },
};
</script>
<style scoped lang="scss">
.graphTrend-pack {
  width: 100%;

  .footer {
    width: 100%;
    height: 32px;
    background: #f5f5f5;
    display: flex;
    align-items: center;
    justify-content: center;
    p {
      font-size: 14px;
      font-weight: bold;
      color: #333333;
    }
  }
}
</style>
